package com.tomdxz.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tomdxz.model.User;
import com.tomdxz.service.TreeService;
import com.tomdxz.serviceimpl.TreeServiceImpl;

import net.sf.json.JSONArray;

public class TreeController extends HttpServlet{

	TreeService tSer = new TreeServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("UTF8");
		resp.setCharacterEncoding("UTF8");
		
		HttpSession session = req.getSession();
		User u = (User)session.getAttribute("User");
		int uId = u.getuId();
		String fId = tSer.findFidByUid(uId);
		JSONArray js = tSer.findTreeByPid(-1,fId);
		PrintWriter pw = resp.getWriter();
		pw.write(js.toString());
		pw.flush();
		pw.close();
		
		
	}
	
}
